﻿using Raffles.OA.Utility;
using Raffles.OA.Data;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Raffles.OA.WebApp.Areas.Manage.Controllers
{
    public class PositionController : BaseController<Position>
    {
        /// <summary>
        /// 获取岗位分页列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public ActionResult GetPageListJson(BaseSearchParam param, Pagination pagination)
        {
            int totalCount = 0;
            var positions = _dbContext.Queryable<Position>().Where(x => !x.DeletedMark)
                                                    .WhereIF(!param.KeyWord.IsEmpty(), x => x.Code == param.KeyWord.Trim() || x.Name == param.KeyWord.Trim())
                                                    .OrderBy(x => x.CreatedOn)
                                                    .ToPageList(pagination.PageIndex, pagination.PageSize, ref totalCount);
            return Content(DataResult<List<Position>>.PageResult(positions, totalCount).ToJson());
        }

        public ActionResult Form(int? id)
        {
            Position position = new Position();
            if (id != null)
            {
                position = _dbContext.Queryable<Position>().InSingle(id);
            }
            return View(position);
        }

        /// <summary>
        /// 岗位保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveForm(PositionDto positionDto)
        {
            try
            {
                if (positionDto.Id == null)
                {
                    if (_dbContext.Queryable<Position>().Any(x => !x.DeletedMark && x.Code == positionDto.Code))
                    {
                        return Json(DataResult.Error("岗位编码不允许重复"));
                    }
                    Position position = new Position();
                    positionDto.Adapt(position); 
                    position.CreatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Insertable<Position>(position).ExecuteCommand();
                }
                else
                {
                    if (_dbContext.Queryable<Position>().Any(x => !x.DeletedMark && x.Code == positionDto.Code && x.Id != positionDto.Id))
                    {
                        return Json(DataResult.Error("岗位编码不允许重复"));
                    }
                    Position position = _dbContext.Queryable<Position>().InSingle(positionDto.Id);
                    positionDto.Adapt(position);
                    position.UpdatedOn = DateTime.Now;
                    position.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Updateable<Position>(position).ExecuteCommand();
                }
                return positionDto.Id == null ? Json(DataResult.Success($"新增成功")) : Json(DataResult.Success($"修改成功"));
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"保存失败,失败原因:{ex.Message}"));
            }
        }

        /// <summary>
        /// 岗位删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteData(int id)
        {
            try
            {
                Position position = _dbContext.Queryable<Position>().InSingle(id);
                position.DeletedMark = true;
                position.UpdatedOn = DateTime.Now;
                position.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                _dbContext.Updateable<Position>(position).ExecuteCommand();
                return Json(DataResult.Success("删除成功"));
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"删除失败,失败原因:{ex.Message}"));
            }
        }

        /// <summary>
        /// 状态改变
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangeStatus(int id)
        {
            Position position = _dbContext.Queryable<Position>().InSingle(id);
            var result = position.EnabledMark;
            position.EnabledMark = !position.EnabledMark;
            position.UpdatedOn = DateTime.Now;
            position.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
            _dbContext.Updateable<Position>(position).ExecuteCommand();
            return result == true ? Json(DataResult.Success("禁用成功")) : Json(DataResult.Success("启用成功"));
        }

        public ActionResult GetPositionList()
        {
            List<Position> positions = _dbContext.Queryable<Position>().Where(x => !x.DeletedMark).OrderBy(x => x.DisplayOrder).ToList();
            return Content(DataResult<List<Position>>.Success(positions).ToJson());
        }
    }
}